package com.example.petshop.service.impl;

import com.example.petshop.entity.User;
import com.example.petshop.mapper.UserMapper;
import com.example.petshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User getUserProfile() {
        // Assuming you have a method in UserMapper to retrieve user profile
        return userMapper.getUserProfile();
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public boolean login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return false;
        }

        // Call UserMapper to fetch user by userName and password
        User user = userMapper.login(username, password);

        // Check if user is found (non-null) to determine login success
        return user != null;
    }

    @Override
    public boolean register(User user) {
        // 检查数据库中是否已存在同名用户
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            // 如果存在同名用户，返回注册失败
            return false;
        }
// 插入用户数据到数据库
        int count = userMapper.insert(user);
        return count > 0;

    }

    @Override
    public String getNickname(String username) {
        return userMapper.getNickname(username);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public boolean updateUserType(Long userId, String userType) {
        int updatedCount = userMapper.updateUserTypeById(userType, userId);

        // 如果更新成功，updatedCount将会大于0
        return updatedCount > 0;
    }
    @Override
    public void createUser(User user) {
        // 使用ShopMapper插入新的商店记录
        userMapper.insert(user);
    }

    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteById(userId);
    }




    @Override
    public void updateUser(User user) {
        // 使用ShopMapper更新商店记录
        userMapper.updateUser(user);
    }

    @Override
    public User getUserById(Integer userId) {
        return null;
    }

    @Override
    public List<User> getAllUser() {
        return null;
    }

    @Override
    public User getUserByNickname(String nickname) {
        return userMapper.findByNickname(nickname);
    }
}



